#summary A brief example for working with Lumenn.

= A Brief Example =

== Defining an Object ===

In order to use any Lumenn object, we must define the object first. Take a look at the following code, and we will explain it step by step later.

{{{
import lumenn.db
import lumenn.validators

class Person(lumenn.db.Object):
	_fields = (
		lumenn.db.Text('firstname', 
			validator = lumenn.validators.Trim, 
			hints	=	lumenn.SINGLELINE, 
			description	=	'First Name'),
		lumenn.db.Text('lastname', 
			validator	=	lumenn.validators.Trim, 
			hints = lumenn.SINGLELINE, 
			description	=	'Last Name'),
		lumenn.db.Int('age', 
			validator	=	lumenn.validators.PositiveInteger, 
			description	=	'Age'),
	)
	
	def Equals(self, o):
		return self.firstname == o.firstname and self.lastname == o.lastname
	
	def Validate(self):
		return self.firstname || self.lastname
}}}

== Creating a New Instance of an Object ==

Now we can use the object as such:

file test.py

{{{
#!/usr/bin/python

import lumenn

# Create a new person object
p = lumenn.Create('phonebook', 'Person')

# Fill in some information
p.firstname = 'John'
p.lastname = 'Doe'

# Save the object
p.Save()

# Once the object has been saved, it will have an ID for future reference
print p.id

}}}

== Searching for a previous object ==

{{{
#!/usr/bin/python

import lumenn

# Create a new person object
p = lumenn.Create('phonebook', 'Person')

# Print everyone whose first name is John
for i in p.firstname.is('John'):
  print i.firstname, i.lastname

# Print everyone whose last name starts with D
for i in p.lastname.Like('D*')
  print i.firstname, i.lastname
  
# Print everyone between the ages of 18 to 25
for i in p.age.between(18, 25):
  print i.firstname, i.lastname

# Direct SQL query. A Person() object will be constructed for each
# row returned as long as the field names exist in the object.
for i in p.Query('SELECT id FROM phonebook_person WHERE firstname = 'John' AND lastname = 'Doe'"):
  print i.id

}}}

== Generating a HTML Form ==

{{{

#!/usr/bin/python

import lumenn

# Create a new person object
p = lumenn.Create('phonebook', 'Person')

print """<form method="post" action="test.html">
<div>Please enter your name:
%s
</div>
</form""" % lumenn.RenderHTML(p, ('firstname', 'lastname') )

}}}

Would result in:

{{{
<form>
<div>Please enter your name:
<table>
<tr><td>First Name:</td><td><input type="text" name="firstname" size="30" /></td></tr>
<tr><td>Last Name:</td><td><input type="text" name="lastname" size="30" /></td></tr>
<tr><td></td><td><input type="submit" name="submit" value="Save" /></td></tr>
</table>
</div>
</form>
}}}